package com.tourlaid.mamagerment.service.impl

import com.tourlaid.mamagerment.entity.Goods
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.mapper.GoodsMapper
import com.tourlaid.mamagerment.service.GoodsService
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/21.
 */
@Service("goodsService")
class GoodsServiceImpl : GoodsService {

    @Resource
    val goodsMapper: GoodsMapper? = null

    override fun queryForList(): List<Goods>? {
        return goodsMapper!!.queryForList()
    }

    override fun insert(goodsName: String?, controlNo: String?) {
        var goods = this.getByName(goodsName)
        if (goods != null) {
            throw TldException(TldExceptionEnum.NAME_EXIST)
        }
        var goods2 = this.getByControlNo(controlNo)
        if (goods2 != null) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        goods = Goods()
        goods.goodsName = goodsName
        goods.controlNo = controlNo
        goods.creator = LoginUtil.userId
        goods.editor = LoginUtil.userId
        goodsMapper!!.insert(goods)
    }

    override fun getByName(goodsName: String?): Goods? {
        if (StringUtils.isEmpty(goodsName)) {
            throw TldException(TldExceptionEnum.NAME_NULL)
        }
        return goodsMapper!!.getByName(goodsName)
    }

    override fun getByControlNo(controlNo: String?): Goods? {
        if (StringUtils.isEmpty(controlNo)) {
            throw TldException(TldExceptionEnum.CONTROLNO_NULL)
        }
        return goodsMapper!!.getByControlNo(controlNo)
    }


    override fun getById(id: Long): Goods {
        var goods = goodsMapper!!.getById(id)
        return goods
    }

    override fun update(id: Long?, goodsName: String?, controlNo: String?) {
        id ?: throw TldException(TldExceptionEnum.ID_NULL)
        var oldGoods = this.getByName(goodsName)
        if (oldGoods != null && !Objects.equals(id, oldGoods.id)) {
            throw TldException(TldExceptionEnum.NAME_EXIST)
        }
        var oldGoods2 = this.getByControlNo(controlNo)
        if (oldGoods2 != null && !Objects.equals(id, oldGoods2.id)) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        var newGoods = Goods()
        newGoods.id = id
        newGoods.goodsName = goodsName
        newGoods.controlNo = controlNo
        newGoods.editor = LoginUtil.userId
        goodsMapper!!.update(newGoods)
    }

    override fun queryByIdList(idList: List<Long?>): List<Goods> {
        return goodsMapper!!.queryByIdList(idList)
    }

    override fun delete(id: Long?) {
        goodsMapper!!.delete(id, LoginUtil.userId)
    }

    override fun queryAllGoods(): List<Goods> {
        return goodsMapper!!.queryAllGoods()
    }
}